package com.icss.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.icss.common.service.BaseServiceImpl;
import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.common.vo.JSONEnum;
import com.icss.dao.IGymOrdersDao;
import com.icss.dao.IGymOrdersGoodsDao;
import com.icss.service.IGoodsDbService;
import com.icss.service.IOrdersDbService;
import com.icss.service.IUsersDbService;
import com.icss.vo.GymCart;
import com.icss.vo.GymOrders;
import com.icss.vo.GymOrdersGoods;
import com.icss.vo.GymGoods;
import com.icss.vo.GymUsers;

/**
 * Service实现类(订单代码)
 * 
 * @author 郝厅
 */
public class OrdersDbServiceImpl extends BaseServiceImpl implements IOrdersDbService
{
	@Resource
	private IGymOrdersDao ordersDao;
	@Resource
	private IGymOrdersGoodsDao ordersGoodsDao;
	
	@Resource
	private IUsersDbService usersDbService;
	@Resource
	private IGoodsDbService goodsDbService;
	
	@Override
	public ApiResponse<Object> saveOneOrdersService(GymOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		int res = this.ordersDao.save(orders);
		if (res > 0)
		{
			double totalPrice = 0 ; 
			/* 存储订单明细 */
			for (Iterator iterator = orders.getCartList().iterator(); iterator.hasNext();)
			{
				GymCart cart = (GymCart) iterator.next();
				GymGoods goods = cart.getProduct(); 
				
				GymOrdersGoods ordersGoods = new GymOrdersGoods();
				ordersGoods.setOrdersId(orders.getId());
				ordersGoods.setProdId(cart.getProdId());
				
				ordersGoods.setName(goods.getName());
				ordersGoods.setPrice(goods.getPrice());
				ordersGoods.setNum(cart.getNum());
				ordersGoods.setTotalPrice(ordersGoods.getPrice() * ordersGoods.getNum());
				
				/* 总价格相加 */
				totalPrice = totalPrice + ordersGoods.getTotalPrice() ; 
				
				ordersGoods.setStatus(orders.getStatus());
				/* 订单商品 */
				this.saveOneOrdersGoodsService(ordersGoods);
				
				cart.setNum(0);
				
				/* 清空购物车 */
				this.usersDbService.updateOneCartService(cart);
				
				/* 更新商品的销量 */
				goods.setSaleNum(goods.getSaleNum() + ordersGoods.getNum());
				goods.setUpdateTime(new Date());
				this.goodsDbService.updateOneGoodsService(goods);
			}
			
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersService(GymOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		orders.setUpdateTime(new Date());
		int res = this.ordersDao.update(orders);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<GymOrders> findCondListOrdersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<GymOrders> apiResponse = new ApiResponse<GymOrders>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<GymOrders> demoOrdersList = Collections.EMPTY_LIST;

		Map<String, List<GymOrders>> dataList = new HashMap<String, List<GymOrders>>();
		demoOrdersList = this.ordersDao.findCondList(pageInfoUtil, condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoOrdersList.iterator(); iterator.hasNext();)
			{
				GymOrders orders = (GymOrders) iterator.next();
				
				extMap.clear();
				extMap.put("id", orders.getUsersId());
				ApiResponse<GymUsers> cateApiResponse = this.usersDbService.findOneUsersService(extMap);
				GymUsers users = cateApiResponse.getDataOneJava();
				orders.setUsers(users);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<GymOrders> findOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<GymOrders> apiResponse = new ApiResponse<GymOrders>();

		Map<String, GymOrders> data = new HashMap<String, GymOrders>();
		GymOrders orders = this.ordersDao.findOne(condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			extMap.clear();
			extMap.put("id", orders.getUsersId());
			ApiResponse<GymUsers> cateApiResponse = this.usersDbService.findOneUsersService(extMap);
			GymUsers users = cateApiResponse.getDataOneJava();
			orders.setUsers(users);
		}
		
		data.put(ApiResponseEnum.NAME_ONE.getName(), orders);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(orders);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneOrdersGoodsService(GymOrdersGoods ordersGoods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		ordersGoods.setCreateTime(new Date());
		int res = this.ordersGoodsDao.save(ordersGoods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersGoods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersGoodsService(GymOrdersGoods ordersGoods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersGoodsDao.update(ordersGoods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersGoods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersGoodsDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<GymOrdersGoods> findCondListOrdersGoodsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<GymOrdersGoods> apiResponse = new ApiResponse<GymOrdersGoods>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<GymOrdersGoods> demoOrdersGoodsList = Collections.EMPTY_LIST;

		Map<String, List<GymOrdersGoods>> dataList = new HashMap<String, List<GymOrdersGoods>>();
		demoOrdersGoodsList = this.ordersGoodsDao.findCondList(pageInfoUtil, condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoOrdersGoodsList.iterator(); iterator.hasNext();)
			{
				GymOrdersGoods ordersGoods = (GymOrdersGoods) iterator.next();
				
				extMap.clear();
				extMap.put("id", ordersGoods.getOrdersId());
				ApiResponse<GymOrders> ordersApiResponse = this.findOneOrdersService(extMap);
				GymOrders orders = ordersApiResponse.getDataOneJava();
				ordersGoods.setOrders(orders);
				
				extMap.clear();
				extMap.put("id", ordersGoods.getProdId());
				ApiResponse<GymGoods> goodsApiResponse = this.goodsDbService.findOneGoodsService(extMap);
				GymGoods product = goodsApiResponse.getDataOneJava();
				ordersGoods.setProduct(product);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersGoodsList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersGoodsList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<GymOrdersGoods> findOneOrdersGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<GymOrdersGoods> apiResponse = new ApiResponse<GymOrdersGoods>();

		Map<String, GymOrdersGoods> data = new HashMap<String, GymOrdersGoods>();
		GymOrdersGoods ordersGoods = this.ordersGoodsDao.findOne(condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			extMap.clear();
			extMap.put("id", ordersGoods.getOrdersId());
			ApiResponse<GymOrders> ordersApiResponse = this.findOneOrdersService(extMap);
			GymOrders orders = ordersApiResponse.getDataOneJava();
			ordersGoods.setOrders(orders);
			
			extMap.clear();
			extMap.put("id", ordersGoods.getProdId());
			ApiResponse<GymGoods> goodsApiResponse = this.goodsDbService.findOneGoodsService(extMap);
			GymGoods product = goodsApiResponse.getDataOneJava();
			ordersGoods.setProduct(product);
		}
		data.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(ordersGoods);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
}
